/**
 * @author LKQ
 * @date 2022/1/7 10:25
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode t1 = new TreeNode(1), t3 = new TreeNode(3), t2 = new TreeNode(2, t1, t3)
                , t7 = new TreeNode(7), t4 = new TreeNode(4, t2, t7);
        solution.insertIntoBST(t4, 5);
    }
    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                if (cur.right == null) {
                    cur.right = new TreeNode(val);
                    break;
                }
                cur = cur.right;
            }else if (cur.val > val) {
                if (cur.left == null) {
                    cur.left = new TreeNode(val);
                    break;
                }
                cur = cur.left;
            }
        }
        return root == null ? new TreeNode(val) : root;
    }
}
